import request from '@/api/request';
import { AxiosPromise } from 'axios';
import {
  BlogQuery,
  ganttQueryParam,
  BlogInfo,
  BlogPost,
  PostDependency,
  PostMilestone,
  GanttDataResponse
} from './types';

// 甘特图相关API
export function fetchGanttData(queryParams: ganttQueryParam): AxiosPromise<GanttDataResponse> {
  return request({
    url: '/api/v1/gantt/data',
    method: 'get',
    params: queryParams
  });
}

export function saveBlogPost(data: Partial<BlogPost>): AxiosPromise<BlogPost> {
  return request({
    url: '/api/v1/gantt/posts',
    method: 'post',
    data
  });
}

export function updateBlogPost(data: Partial<BlogPost>): AxiosPromise<BlogPost> {
  return request({
    url: `/api/v1/gantt/posts/${data.id}`,
    method: 'put',
    data
  });
}

export function updatePostTimeline(
  updates: Array<{
    id: string;
    startDate: string;
    endDate: string;
  }>
): AxiosPromise<void> {
  return request({
    url: '/api/v1/gantt/posts/timeline',
    method: 'patch',
    data: { updates }
  });
}

export function createMilestone(data: Partial<PostMilestone>): AxiosPromise<PostMilestone> {
  return request({
    url: '/api/v1/gantt/milestones',
    method: 'post',
    data
  });
}

export function createDependency(data: Partial<PostDependency>): AxiosPromise<PostDependency> {
  return request({
    url: '/api/v1/gantt/dependencies',
    method: 'post',
    data
  });
}

export function deleteDependency(dependencyId: string): AxiosPromise<void> {
  return request({
    url: `/api/v1/gantt/dependencies/${dependencyId}`,
    method: 'delete'
  });
}

/**
 * 获取所有里程碑数据
 * @returns 包含里程碑列表的Promise
 */
export function getMilestones(): AxiosPromise<PostMilestone[]> {
  return request({
    url: '/api/v1/gantt/milestones',
    method: 'get'
  });
}

/**
 * 获取所有依赖关系数据
 * @returns 包含依赖关系列表的Promise
 */
export function getDependencies(): AxiosPromise<PostDependency[]> {
  return request({
    url: '/api/v1/gantt/dependencies',
    method: 'get'
  });
}
